<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/functions.php' );

require_once( '../../libs/login.inc.php' );



define( 'IMG_DIR', '../kats-blog/images/' );

define( 'TEMPORARY_DIR', '../../temporary/' );





if( !in_array( "L", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



$db = new Class_DB;



if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	

	$phase = 'input';

	

	if( isset($_GET['id']) && $_GET['id'] != '' ){

		

		if( is_numeric( $_GET['id'] ) ){

			

			$sql = sprintf("SELECT * FROM D_BLOG WHERE id = %s" , $_GET['id'] );

			$res = $db->query( $sql );

			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );

			$forms['blog_date'] = str_replace('-', '/', $forms['blog_date'] );

			if( $forms['filename'] != '' ){

				$tmp_name = md5( microtime() ) . strtolower( $forms['filename'] );

				copy( IMG_DIR . $forms['filename'], TEMPORARY_DIR . $tmp_name );

				$forms['temp_name'][0] = $tmp_name;

			}else{

				$forms['temp_name'][0] = '';

			}

			

		}

		

	}else{

		$forms['status'] = 1;

		$forms['blog_date'] = date("Y/n/d");

	}



}else{

	

	$forms = $_POST;



	if( $forms['mode'] == 'delete' ){

		//削除

		$sql = sprintf( "SELECT filename FROM D_BLOG WHERE id = %u", $forms['id'] );

		$db->query( $sql );

		$row = $db->fetch();

		if( file_exists( IMG_DIR . $row['filename'] ) ){

			unlink( IMG_DIR . $row['filename'] );

		}

		

		$sql = sprintf( "DELETE FROM D_BLOG WHERE id = %u", $forms['id'] );

		$db->query( $sql );

		$phase = 'complete';

		$smarty->assign('message', '削除が完了しました。');

	}





	// 入力画面から

	if( $forms['mode'] == 'input' ){

		$err = new Class_ERROR;

		

		$err_mes['title']		= $err->check( $forms['title'], array("EXIST") );

		$err_mes['blog_date']	= $err->check( $forms['blog_date'], array("EXIST", "YMD") );

		$err_mes['page_text']	= $err->check( $forms['page_text'], array("EXIST") );



		if( $err->clear ){

			$phase = 'confirm';

			$tran_id = sha1( microtime(true) );



			$_SESSION['TRAN_ID'] = $tran_id;

			$smarty->assign('tran_id', $tran_id );

		}else{

			$phase = 'input';

		}



	}



	// 戻るボタン

	if( $forms['mode'] == 'back' ){

		$phase = 'input';

	}



	// 確認画面から

	if( $forms['mode'] == 'confirm' ){

		

		$ym = substr( $forms['blog_date'], 0, 4 ) . substr( $forms['blog_date'], 5, 2 );

		

		if( $forms['id'] == '' ){

			//新規登録

			if( $forms['tran_id'] == $_SESSION['TRAN_ID'] ){



				$sql = sprintf( "INSERT INTO D_BLOG(status, ym, blog_date, title, page_text, r_date, u_date) VALUES('%s', '%s', '%s', '%s', '%s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP )",

					$forms['status'],

					$ym,

					$db->quote( $forms['blog_date'] ),

					$db->quote( $forms['title'] ),

					$db->quote( $forms['page_text'] )

				);

				$db->query( $sql );

				$id = $db->last_id();

				

			}

			$phase = 'complete';

			$smarty->assign('message', '登録が完了しました。');

		}else{

			//更新

			if( $forms['tran_id'] == $_SESSION['TRAN_ID'] ){

				

				$sql = sprintf( "UPDATE D_BLOG SET status = '%s', ym = '%s', blog_date = '%s', title = '%s', page_text = '%s', u_date = CURRENT_TIMESTAMP WHERE id = %u", 

					$forms['status'],

					$ym,

					$db->quote( $forms['blog_date'] ),

					$db->quote( $forms['title'] ),

					$db->quote( $forms['page_text'] ),

					$forms['id']

				);

				$db->query( $sql );

				$id = $forms['id'];

				

			}

			$phase = 'complete';

			$smarty->assign('message', '更新が完了しました。');

		}



		// 画像

		if( $forms['tran_id'] == $_SESSION['TRAN_ID'] ){

			if( isset( $forms['temp_name'][0] ) && $forms['temp_name'][0] != '' ){

				$fname = $id . getext( $forms['temp_name'][0] );

				resize( TEMPORARY_DIR . $forms['temp_name'][0], IMG_DIR . $fname, 684, 684 );

				unlink( TEMPORARY_DIR . $forms['temp_name'][0] );

				//rename( TEMPORARY_DIR . $forms['temp_name'][0], IMG_DIR . $fname );

				$sql = sprintf("UPDATE D_BLOG SET filename = '%s' WHERE id = %u", $fname, $id );

				$db->query( $sql );

			}

		}

		$_SESSION['TRAN_ID'] = '';

	}



	// ファイルアップロード

	if( $forms['mode'] == 'upload' ){

		

		if( is_array( $_FILES['file']['error'] ) ){



			foreach( $_FILES['file']['error'] as $k => $v ){



				if( $v == 1 ){

					$err = new Class_ERROR;

					$err_mes['file'][$k] = $err->set_error('ファイルサイズ大きいため、アップロードができませんでした');

					$forms['temp_name'][$k] = '';

				}elseif( $v == 0 ){



					$ext = strtolower( getext( $_FILES['file']['name'][$k] ) );

					switch( $ext ){

					case '.jpg':

					case '.jpeg':

					case '.gif':

					case '.png':

						$is_img = 1;

						break;

					default:

						$is_img = 9;

					}



					if( $k == 0 && $is_img == 9 ){

						$err = new Class_ERROR;

						$err_mes['file'][$k] = $err->set_error('アップロードしたファイルは扱えませんでした');

						$forms['temp_name'][$k] = '';

					}else{



						if( is_uploaded_file($_FILES['file']['tmp_name'][$k]) ){

							

							$tmp_name = md5( microtime() ) . strtolower( getext( $_FILES['file']['name'][$k]) );

							if( move_uploaded_file( $_FILES['file']['tmp_name'][$k], TEMPORARY_DIR . $tmp_name) ){

								chmod( TEMPORARY_DIR . $tmp_name, 0644);

							}

							$forms['file_size'][ $k ] = filesize( TEMPORARY_DIR . $tmp_name );

							$forms['temp_name'][ $k ] = $tmp_name;

							$forms['original'][ $k ] = $_FILES['file']['name'][$k];

							$forms['is_img'][ $k ] = $is_img;



							

						}

					}

				}

			}

		}



		$phase = 'input';



	}



	// 画像削除

	if( $forms['mode'] == 'remove_img' ){



		if( file_exists( TEMPORARY_DIR . $forms['temp_name'][0] ) ){

			unlink( TEMPORARY_DIR . $forms['temp_name'][0] );

		}

		$forms['temp_name'][0] = '';

		$phase = 'input';

	}

	

}







if( $phase == 'input' )		$smarty->assign('message', '内容を確認し、確認ボタンをクリックしてください。');

if( $phase == 'confirm' )	$smarty->assign('message', 'この内容で登録します。よろしいですか？');



//$smarty->assign('title', $title);

$smarty->assign('phase', $phase);

if( isset( $forms ) ) $smarty->assign('forms', $forms);

if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);



$smarty->display('web-admin/blog_form.html');



?>